package com.sdgakj.rest;

/**
 * Created by 鲁俊杰 on 2017/4/18.
 * 个人历程控制层
 */

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.sdgakj.entity.History;
import com.sdgakj.mvc.framework.util.ConditionUtil;
import com.sdgakj.service.history.HistoryService;
import com.sdgakj.sql.OptimizeSelectSqlImpl;
import com.sdgakj.web.manager.RoleController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/history")
public class HistoryRestController {

    @Autowired
    private HistoryService historyService;

    private static Logger logger = LoggerFactory.getLogger(RoleController.class);

    private static final String PAGE_SIZE = "10";

    private static Map<String,String> sortTypes = Maps.newLinkedHashMap();

    static {
        sortTypes.put("auto", "自动");
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONObject list(@RequestParam(value = "history",defaultValue = "") String jsonData,
                 @RequestParam(value = "pId") Long personId,
                 @RequestParam(value = "pageNo" , defaultValue = "1") int pageNo,
                 @RequestParam(value = "pageSize" , defaultValue = PAGE_SIZE) int pageSize,
                 @RequestParam(value = "sortType", defaultValue = "auto") String sortType
                  //         ,ServletRequest request
    ) {
        JSONObject result = new JSONObject();
        if (personId==0) {
            result.put("resCode", "0");
            result.put("resMsg", "请求参数错误!");
            return result;
        }
        History history = null;
        if (!"".equals(jsonData)){
            try {
                history=JSONObject.parseObject(jsonData,History.class);
                history.setFhTitle(history.getFhTitle());
            }catch (Exception e){
                result.put("resCode", "0");
                result.put("resMsg", "JSON解析错误!");
                return result;
            }
        }
        //Map<String,Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Map<String, Object> searchParams = new HashMap<>();
        if(history!=null){
            if (history.getFhTitle()!=null&&!"".equals(history.getFhTitle())){
                searchParams.put(ConditionUtil.Operator.LIKE + "fh_title", history.getFhTitle());
            }
        }
        return historyService.getHistoryByPage(searchParams, pageNo, pageSize, sortType);
    }

    /**
     * 添加个人历程
     * @param jsonData
     * @return
     */
    @RequestMapping(value="/create",method= RequestMethod.POST)
    public JSONObject createHistory(@RequestParam(value = "history",defaultValue = "") String jsonData){
        JSONObject result=new JSONObject();
        History history=null;
        try{
            history = JSONObject.parseObject(jsonData,History.class);
        }catch (Exception e){
            result.put("resCode", "0");
            result.put("resMsg", "JSON参数解析异常!");
            return result;
        }
        return historyService.createHistory(history);
    }

    /**
     * 修改个人历程
     * @param jsonData
     * @return
     */
    @RequestMapping(value="/update",method= RequestMethod.POST)
    public JSONObject update(@RequestParam(value = "history",defaultValue = "") String jsonData){
        JSONObject result=new JSONObject();
        History history = null;
        try {
           history=JSONObject.parseObject(jsonData,History.class);
        } catch (Exception e) {
            result.put("resCode", "0");
            result.put("resMsg", "JSON参数解析异常!");
            return result;
        }

        History history2 = null;
        if (history!=null&&history.getId()!=0){
            history2=historyService.getHistoryById(history.getId());
        }
        history2.setFhTitle(history.getFhTitle());
        history2.setFhDesp(history.getFhDesp());
        history2.setUpdatetime(new java.sql.Timestamp(new java.util.Date().getTime()));

        historyService.updateHistory(history2);
        return result;
    }

    /**
     * 根据id删除个人历程
     * @param persenId
     * @return
     */
    @RequestMapping(value = "/delete/{id}")
    public JSONObject delete(@PathVariable(value = "id") Long persenId){
        JSONObject result=new JSONObject();
        if (persenId==0){
            result.put("resCode","0");
            result.put("resMsg","请求参数异常！");
        }
       return historyService.deleteHistoryById(persenId);
    }

    /**
     * 根据id查询个人历程的详情
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public JSONObject detail(@PathVariable(value = "id") Long persenId){
        JSONObject result=new JSONObject();
        if (persenId==0){
            result.put("resCode","0");
            result.put("resMsg","请求参数异常！");
        }
        History history=historyService.getHistoryById(persenId);
        result.put("history",history);
        return result;
    }

}
